<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="../../include/admincontent.jsp" %>
<div id="Content">
    <form id="excelValid" method="post" action="/DUserResume/MatchingPostAction">
        <input type="hidden" id="h_pid" name="pid" value="${pid}"/>
        <input type="hidden" id="h_fid" name="fid" value="${fid}"/>
        <input type="hidden" id="h_deptcode" name="deptcode" value="${deptcode}"/>

        <table class="treeTable searchform">
            <tbody>
            <tr>
                <td width="120">导入匹配人员名单：</td>
                <td width="400">
                    <div><input type="file" name="excel" datatype="*" nullmsg="请选择Excel文件"/></div>
                </td>
                <td>
                    <button class="layui-btn site-demo-active" id="btn_import" data-type="loading">开始匹配</button>
                    <%--  <button type="submit" id="btn_import" class="op">导入数据</button>--%>
                </td>

            </tr>
            <tr>
                <td colspan="3">
                <div>  <b>注意：</b>系统自动识别身份证号和岗位(<b style="color: red">首行列中以身份证和岗位开始的列</b>)</div>
                </td>
            </tr>
            </tbody>
        </table>
    </form>
    <table class="treeTable" id="recommendTable" style="margin-top:5px;">
        <thead>
        <tr>
            <td width="20"><input type="checkbox" class="allcheckbox" title="选择全部/取消全选"/></td>
            <td width="80">姓名</td>
            <td width="60">性别</td>
            <td>证件号码</td>
            <td width="120">原应聘职位</td>
            <td width="120">现应聘职位</td>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

<div class="layui-progress" lay-showpercent="true">
    <div class="layui-progress-bar" lay-percent="20%"></div>
</div>
<script src="/resources/js/jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
    var time;
    var lindex;
    var DISABLED = 'layui-btn-disabled';
    layui.use('element', function(){
        var element = layui.element;
    });
    layui.use('element', function(){
        var $ = layui.jquery
            ,element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
        var downurl="";
        //触发事件
        var active = {
            loading: function(othis){

                lindex=  layer.alert('  <div><div id="importResult"></div><div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="excelprogress">\n'
                    +'    <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>\n'
                    +'</div></div>',{"btn":false,"title":"数据匹配中请耐心等待..."});
                if(othis.hasClass(DISABLED)) return;

                //模拟loading
                var n = 0;
                timer = setInterval(function(){
                    $.ajax({
                        url:'/ExcelImport/getProcess',
                        dataType:'JSON',
                        type:'POST',
                        success:function (json) {
                            if (json.datas!=null){
                                var totalNum=json.datas.totalNum;
                                var successNum=json.datas.successNum;
                                var errorNum=json.datas.errorNum;
                                n=((successNum+errorNum)/totalNum)*100;
                                //n = n + Math.random()*10|0;
                                if(n>=100){
                                    n = 100;
                                    clearInterval(timer);
                                    othis.removeClass(DISABLED);
                                    layer.close(lindex);
                                    layer.alert("匹配完成！共"+successNum+"条数据导入成功！"+errorNum+"条数据导入失败！"
                                        ,{"btn":[ '关闭']

                                        });
                                }
                                $('#importResult').empty();
                                $('#importResult').append('<span>共'+totalNum+'条数据。'+successNum+'条数据导入成功，'+errorNum+'条数据导入失败！</span>');
                                element.progress('excelprogress', n+'%');
                            }
                        }
                    });


                }, 1000);

                othis.addClass(DISABLED);
            }
        };
        $("#excelValid").Validform({
            ajaxPost: true,
            beforeSubmit: function (curform) {
                var othis = $('#btn_import'), type = $('#btn_import').data('type');
                active[type] ? active[type].call($('#btn_import'), othis) : '';
                curform.ajaxSubmit({
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        //data = $.parseJSON($(data).text());
                        if (data.state){
                            //clearInterval(timer);
                            downurl=data.url;
                            var datas=data.datas;
                            if(!!datas){
                                $('#recommendTable tbody').empty();
                                $('#recommendTable tfoot').empty();
                                $(datas).each(function (index,resume) {
                                    var tr=$("<tr></tr>");
                                    $(tr).append("<td><input type='checkbox' id='cb_" + resume.ID + "' njid="+resume.NEWJID+" njname="+resume.NEWJNAME+" /></td><td >"+resume.NAME+"</td><td >"+resume.SEXNAME+"</td><td >"+resume.CARDNO+"</td><td >"+resume.OLDJNAME+"</td><td >"+resume.NEWJNAME+"</td>")
                                    $('#recommendTable tbody').append(tr);
                                });
                               var btn_startRecommend=$("<button class=\"layui-btn site-demo-active\" id=\"btn_startRecommend\" >确认转岗</button>");
                                btn_startRecommend.on('click',function () {
                                    var checkedResume=   $('#recommendTable tbody tr').find("input[type='checkbox']:checked ");
                                    if(checkedResume.length<=0){
                                        layer.alert("最少选择一条数据！");
                                    }else{
                                        var cfindex= layer.confirm('1、简历筛选后的流转信息及发布信息将会被清除!<br>2、如需要审核,审核完成后简历筛选后的流转信息将会被清除!<br>3、简历筛选后的相关数据请重新输入！<br>确定要进行转岗操作吗？', {
                                            title:"温馨提示",
                                            btn: ['是', '否']
                                        }, function () {
                                            var successIndex=0;
                                            $(checkedResume).each(function (index,value) {
                                                var ckid=$(value).attr("id");
                                                var newJID=$(value).attr("njid");
                                                var newnJname=$(value).attr("njname");
                                                var userid=ckid.substring(3,ckid.length);
                                                var pid=$('#h_pid').val();
                                                var fid=$('#h_fid').val();
                                                var deptcode=$('#h_deptcode').val();
                                                $.post("/DUserResume/RecommendPostAction",{fid:fid,pid:pid,deptcode:deptcode,gwcode:newJID,gwname:newnJname,id:userid,needComfirm:"无需确认",needReplan:"NO"},function (json) {
                                                    if(json.state){
                                                        successIndex++;
                                                        if(successIndex==checkedResume.length){
                                                            layer.close(cfindex);
                                                            parent.getList();
                                                            parent.layer.alert("操作成功！");
                                                            setTimeout(parent.layer.closeAll,3000);
                                                        }
                                                    }

                                                },"JSON");
                                            });

                                            parent.layer.close(cfindex);
                                            //parent.layer.closeAll();
                                        }, function () {
                                            parent.layer.close(cfindex);
                                        });



                                    }
                                });
                                $('#Content').append(btn_startRecommend);
                            }
                        } else {
                            clearInterval(timer);
                            layer.alert(data.message);
                            layer.close(lindex);
                            $('#btn_import').removeClass(DISABLED)
                        }
                    }
                });
                return false;
            }
        });

        $('#btn_import').on('click', function(){
            $("#excelValid").submit();
            //表单提交
            return false;
        });



    });


</script>

</body>
</html>